/*
 * Created on 13/08/2004
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package br.com.site.model.entity;

import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import br.com.site.model.dao.DAOException;
import br.com.site.model.dao.DAOFactory;
import br.com.site.model.dao.LinkDAO;
import br.com.site.validation.Annotation.Validar;

/**
 * @author pgr-eduardo
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
@Entity
public class Link implements CrudBaseEntity<Link, Long>, Auditoria{
	
	@Id @GeneratedValue(strategy=GenerationType.IDENTITY)
	private Long id;
	
	@ManyToOne(fetch=FetchType.LAZY, targetEntity=br.com.site.model.entity.Estande.class)	
    @JoinColumn(name="estande")
	private Estande estandeLink;
	
	@Column(columnDefinition="text")
	private String descricao;
	
	@Validar(type=Validar.ValidationType.URL)
	private String url;
	
	/*Auditoria*/  
	  @Column(name = "loginUsuario", nullable = false)
	  private String loginUsuario;
	  
	  @Column(name = "dtCriacao", nullable = false)
	  private Date dtCriacao;
	  
	  @Column(name = "dtModificacao", nullable = false)
	  private Date dtModificacao;
	  
	  @Column(name = "ativo", nullable = false)
	  private boolean ativo;


	/**
	 * @return Returns the descricao.
	 */
	public String getDescricao() {
		return descricao;
	}
	/**
	 * @param descricao The descricao to set.
	 */
	public void setDescricao(String descricao) {
		this.descricao = descricao;
	}

	
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	/**
	 * @return Returns the url.
	 */
	public String getUrl() {
		return url;
	}
	/**
	 * @param url The url to set.
	 */
	public void setUrl(String url) {
		this.url = url;
	}
	public Estande getEstandeLink() {
		return estandeLink;
	}
	public void setEstandeLink(Estande estandeLink) {
		this.estandeLink = estandeLink;
	}
	public String getLoginUsuario() {
		return loginUsuario;
	}
	public void setLoginUsuario(String loginUsuario) {
		this.loginUsuario = loginUsuario;
	}
	public Date getDtCriacao() {
		return dtCriacao;
	}
	public void setDtCriacao(Date dtCriacao) {
		this.dtCriacao = dtCriacao;
	}
	public Date getDtModificacao() {
		return dtModificacao;
	}
	public void setDtModificacao(Date dtModificacao) {
		this.dtModificacao = dtModificacao;
	}
	public boolean isAtivo() {
		return ativo;
	}
	public void setAtivo(boolean ativo) {
		this.ativo = ativo;
	}
	
	@Override
	public void remove() throws BusinessException {
		DAOFactory factory = DAOFactory.getInstance();
		try{
			factory.txBegin();
			LinkDAO dao = factory.getDao(LinkDAO.class);			
			dao.remove(this.id);

			factory.txCommit();
		}catch (DAOException e){
			factory.txRollback();
			throw new BusinessException(e);
		}finally{
			factory.shutdown();
		}
		
	}
	@Override
	public void save() throws BusinessException {
		
		DAOFactory factory = DAOFactory.getInstance();
		try{
			factory.txBegin();
			LinkDAO dao = factory.getDao(LinkDAO.class);			
			dao.save(this);

			factory.txCommit();
		}catch (DAOException e){
			factory.txRollback();
			throw new BusinessException(e);
		}finally{
			factory.shutdown();
		}
	}
	
	
	
	public static Link find(Long oid){
		DAOFactory factory = DAOFactory.getInstance();
		LinkDAO dao = factory.getDao(LinkDAO.class);
		
		return dao.find(oid);
	}
	public static List<Link> find(){
		DAOFactory factory = DAOFactory.getInstance();
		LinkDAO dao = factory.getDao(LinkDAO.class);

		return dao.find();
	}
	
	public static List<Link> findByCriteria(Link link){
		DAOFactory factory = DAOFactory.getInstance();
		LinkDAO dao = factory.getDao(LinkDAO.class);

		return dao.findByCriteria(link);
	}
	
	public static List<Link> find(int first, int max){
		DAOFactory factory = DAOFactory.getInstance();
		LinkDAO dao = factory.getDao(LinkDAO.class);

		return dao.find(first, max);
	}
	
	public static List<Link> findByCriteria(Link link, int first, int max)
	{
		DAOFactory factory = DAOFactory.getInstance();
		LinkDAO dao = factory.getDao(LinkDAO.class);

		return dao.findByCriteria(link, first, max);
		
	}
 
	
}
